/*
 * Name: Philippe Gambling
 * Course: CSCI 5733
 * Assignment: Homework 1
 * Created: 6/15/2009
 * Modified: 6/15/2009
 *
 * Class Name: RequestHandler
 * Description: Handles all incoming HTTP requests
 *              for news topics, and sends the XML response.
 * 
 */

package csci5733.hw1;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Handles incoming requests for a topic and builds the XML response.
 * @author Philippe Gambling
 */
public class RequestHandler extends HttpServlet {
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            NewsRetriever newsRtrv = new NewsRetriever();
            setupNewsRtrv(newsRtrv);
            String topic = request.getParameter("topic");

            if(topic != null) newsRtrv.fetchNews(topic);
            if(newsRtrv.hasNews()) {
                response.setContentType("text/xml");
                out.println(newsRtrv.getXML());
            } else {
                out.println("<p>No news!</p>");
            }
        } catch (Exception ex) {
            out.println("<p>Something blew up!</p>");
            ex.printStackTrace(out);
        } finally { 
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void setupNewsRtrv(NewsRetriever newsRtrv) {
        NewsSource cnet = new CnetSource(new NewsUrl("http://dcm.uhcl.edu/yue/courses/xml/notes/general/cnetnews.pl?topic="));
        NewsSource moreover = new MoreoverSource(new NewsUrl("http://dcm.uhcl.edu/yue/courses/xml/notes/general/newsCSV.pl?topic="));
        newsRtrv.addNewsSource(cnet);
        newsRtrv.addNewsSource(moreover);
    }

}
